# encoding: utf-8

import time
import uuid
import datetime
import re
import json
from hashlib import sha1, sha256

import scrapy
from scrapy import Request, FormRequest

try:
    from App_Spd.items import AppSpdItem
    from App_Spd.utils import analysis, get_urlid, get_pubtime
except:
    from app_spider.items import AppSpdItem
    from app_spider.utils import analysis, get_urlid, get_pubtime

headers = {
    'Host': 'proxy.cztvcloud.com',
    'type': 'fusion',
    'user-agent': 'okhttp/4.7.2',
}


class ZhangshangsongyangAppSpider(scrapy.Spider):
    websiteId = '2141422582'
    pubSource = '掌上松阳'
    name = 'zhangshangsongyang_app'

    list_url = 'https://proxy.cztvcloud.com/api/fusion/channels/getChannelsInfo?appId=1028&channelId=12&page={}&platform=Android&appVersion=2.3.7'
    # detail_url = 'https://i.cztvcloud.com/news/{}?app_id=8a3a171d4405ac4ed05fd00e66e8c059&signature=e4933f3ce0c0d3d05a779da540d4836729c318ae&timestamp={}&client_id=1afac8301cd55dd52c5f0c59f8195e2c&app_version=1.0.7&client_type=android&token='
    detail_url = 'https://proxy.cztvcloud.com/api/fusion/news/getNewsInfo?appId=1028&newsId={}&platform=Android&appVersion=2.3.7'

    def start_requests(self):
        for i in range(1, 10):
        # for i in range(1, 2):
            yield Request(url=self.list_url.format(i))

    def parse(self, response, **kwargs):
        resp = response.json()
        for its in resp['data']['block'][0]['items']:
            yield Request(url=self.detail_url.format(its['id'], int(time.time())), callback=self.parse_detail)

    def parse_detail(self, response):
        resp = response.json()
        # print(resp)
        data = resp['data']
        item = AppSpdItem()
        item['title'] = data.get('title', '')
        item['author'] = data.get('authorName', '')
        item['url'] = response.url

        item['pubtime'] = str(datetime.datetime.fromtimestamp(data['createdAt']))
        item['content'] = analysis(data['content'])
        item['websiteId'] = 2141422582  # 提供app对应的id
        item['pubSource'] = "掌上松阳"  # 提供app对应名称
        item['urlid'] = get_urlid(item['url'])

        yield item
